home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE10 / COLLECTS / PIEGRPE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-13  |  6.1 KB  |  201 lines

  1. unit piegrpe;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  7.   Dialogs, PieGraf, DsgnIntF, TypInfo, StdCtrls, Mask, ColorGrd;
  8.  
  9. type
  10.  
  11.   TPieGraphEditor = class(TForm)
  12.     Label1: TLabel;
  13.     ValuesListBox: TListBox;
  14.     Label2: TLabel;
  15.     AddBtn: TButton;
  16.     ColorGrid1: TColorGrid;
  17.     Label3: TLabel;
  18.     RemoveBtn: TButton;
  19.     OkBtn: TButton;
  20.     CancelBtn: TButton;
  21.     NewValue: TEdit;
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure FormDestroy(Sender: TObject);
  24.     procedure ValuesListBoxDrawItem(Control: TWinControl; Index: Integer;
  25.       Rect: TRect; State: TOwnerDrawState);
  26.     procedure AddBtnClick(Sender: TObject);
  27.     procedure RemoveBtnClick(Sender: TObject);
  28.     procedure CancelBtnClick(Sender: TObject);
  29.     procedure NewValueKeyPress(Sender: TObject; var Key: Char);
  30.   private
  31.     FPiePieces: TPiePieces;
  32.     FPieGraphic: TPieGraphic; // Used as a Backup
  33.     Modified: Boolean;
  34.     procedure UpdateValuesListBox;
  35.   end;
  36.  
  37.   { Now declare the TPropertyEditor descendant and override the
  38.    required methods }
  39.   TPiePiecesProperty = class(TPropertyEditor)
  40.     function GetAttributes: TPropertyAttributes; override;
  41.     function GetValue: String ; override;
  42.     procedure Edit; override;
  43.   end;
  44.  
  45. { This function will be called by the property editor's Edit method. }
  46. function EditPiePieces(PiePieces: TPiePieces): Boolean;
  47.  
  48. var
  49.   PieGraphEditor: TPieGraphEditor;
  50.  
  51. implementation
  52. {$R *.DFM}
  53.  
  54. function IsCharNumeric(C: Char): Boolean;
  55. { This helper function determines whether or not a character is numeric }
  56. var
  57.   Code, V: Integer;
  58. begin
  59.   Val(C, V, Code);
  60.   Result := Code = 0;
  61. end;
  62.  
  63. function EditPiePieces(PiePieces: TPiePieces): Boolean;
  64. { Instanciates the TPieGraphEditor dialog which directly modifies
  65.   the TPiePieces collection. }
  66. begin
  67.   with TPieGraphEditor.Create(Application) do begin
  68.     try
  69.       FPiePieces := PiePieces; // Point to the actual TPiePieces collection
  70.       { Copy the TPiePieces to the backup FPieGraphic which will be
  71.         used as a backup in case the user cancels }
  72.       FPieGraphic.PiePieces.Assign(PiePieces);
  73.       { Draw the listbox with the list of TPiePieces Values. }
  74.       UpdateValuesListBox;
  75.       ShowModal; // Display the form.
  76.       Result := Modified;
  77.     finally
  78.       Free;
  79.     end;
  80.   end;
  81. end;
  82.  
  83. { TPieGraphEditor }
  84.  
  85. procedure TPieGraphEditor.UpdateValuesListBox;
  86. { Updates the Values list box with updated information from the FPiePieces
  87.   instance. The listbox stores the TPiePieces color in it's Objects array
  88.   and then uses this in it's OnDrawItem event handle to draw the text in the
  89.   specified color. }
  90. var
  91.   i: Integer;
  92. begin
  93.   ValuesListBox.Clear; // First clear the list box.
  94.   for i := 0 to FPiePieces.Count - 1 do
  95.     with FPiePieces[i] do
  96.       ValuesListBox.Items.AddObject(IntToStr(WedgeValue), Pointer(Color));
  97. end;
  98.  
  99. procedure TPieGraphEditor.FormCreate(Sender: TObject);
  100. { The OnCreate event handler instanciates a TPiePieces instance to hold a
  101.   backkup of the original TPieWedge values. This allows the user to cancel
  102.   any edits made. }
  103. begin
  104.   FPieGraphic := TPieGraphic.Create(self);
  105. end;
  106.  
  107. procedure TPieGraphEditor.FormDestroy(Sender: TObject);
  108. { Free the TPieGraphic instance. }
  109. begin
  110.   FPieGraphic.Free;
  111. end;
  112.  
  113. procedure TPieGraphEditor.ValuesListBoxDrawItem(Control: TWinControl;
  114.   Index: Integer; Rect: TRect; State: TOwnerDrawState);
  115. { Uses an owner-draw list box to draw the TPieWedge values in their specified
  116.   color }
  117. begin
  118.   with ValuesListBox do begin
  119.     Canvas.FillRect(Rect);
  120.     Canvas.Font.Color := TColor(Items.Objects[Index]);
  121.     DrawText(Canvas.Handle, PChar(Items[Index]),
  122.       Length(Items[Index]), Rect, dt_Left or dt_VCenter);
  123.    end;
  124. end;
  125.  
  126. procedure TPieGraphEditor.AddBtnClick(Sender: TObject);
  127. { Adds a new TPieWedge to the collection and reflects the new information
  128.   in the ValuesListBox }
  129. var
  130.   PieWedge: TPieWedge;
  131. begin
  132.   If StrToInt(NewValue.Text) > 0 then begin
  133.     ValuesListBox.Items.Add(NewValue.Text);
  134.     ValuesListBox.Refresh;
  135.     PieWedge := FPiePieces.AddPiece(StrToInt(NewValue.Text),
  136.       ColorGrid1.ForegroundColor);
  137.     Modified := True;
  138.   end;
  139. end;
  140.  
  141. procedure TPieGraphEditor.RemoveBtnClick(Sender: TObject);
  142. { Removes the selected TPieWedge from the collection }
  143. var
  144.   i: integer;
  145. begin
  146.   i := ValuesListBox.ItemIndex;
  147.   if i >= 0 then begin
  148.     ValuesListBox.Items.Delete(i);   // Remove the item from the listbox
  149.     FPiePieces[i].Free;              // Remove the item from the collection
  150.     Modified := True;
  151.   end;
  152. end;
  153.  
  154. procedure TPieGraphEditor.CancelBtnClick(Sender: TObject);
  155. { Cancels any edits made to the collection. Copies the backup of the original
  156.   TPiePieces collection back to the TPiePieces property. }
  157. begin
  158.   FPiePieces.Assign(FPieGraphic.PiePieces);
  159.   Modified := False;
  160.   ModalResult := mrCancel;
  161. end;
  162.  
  163. procedure TPieGraphEditor.NewValueKeyPress(Sender: TObject; var Key: Char);
  164. { This method ensures that any characters entered into the TEdit control are
  165.   numeric only. }
  166. begin
  167.   if not IsCharNumeric(Key) then
  168.     Key := #0;
  169. end;
  170.  
  171. { TPiePiecesProperty }
  172.  
  173. function TPiePiecesProperty.GetAttributes: TPropertyAttributes;
  174. { Tell the Object Inspector that the property editor will use a
  175.   dialog. This will cause the Edit method to be invoked when the user
  176.   clicks the elipsis button in the Object Inspector. }
  177. begin
  178.   Result := [paDialog];
  179. end;
  180.  
  181. procedure TPiePiecesProperty.Edit;
  182. { Invoke the EditPiePieces() method and pass in the reference to the
  183.   TPiePieces's instance being edited. This reference can be obtain by
  184.   using the GetOrdValue method. Then redraw the PieGraphic by calling
  185.   the TRunButtons.UpdatePiePieces method. }
  186. begin
  187.   if EditPiePieces(TPiePieces(GetOrdValue)) then begin
  188.     Modified;
  189.   end;
  190.   TPiePieces(GetOrdValue).UpdatePiePieces;
  191. end;
  192.  
  193. function TPiePiecesProperty.GetValue: String;
  194. { Override the GetValue method so that the class type of the property
  195.   being edited is displayed in the Object Inspector. }
  196. begin
  197.   Result := Format('(%s)', [GetPropType^.Name]);
  198. end;
  199.  
  200. end.
  201.